<?php

namespace App\Console\Commands;

use App\Http\Model\FinanceKk;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class PksendGift extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'PksendGift  {--data=}';
    protected $redis_cache_finance_user_wallet = "CACHE:FINANCE:USER:WALLET";  //CACHE:FINANCE:USER:WALLET:{useid} => {value}
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每日PK结算金币10%';
    protected $financeModel;//金币结算模型
    protected $union_table = 'union'; //公会model
    protected $union_room_table = 'union_room';//公会房间
    protected $finance_union_room_stream_table = 'finance_union_room_stream';//公会房间流水
    protected $gift_steam_table = 'gift_stream';//送礼记录表
    protected $user_table = 'user';//用户表

    protected $log = 'union_pk';//pk日志
    protected $errorLog = 'error_union_pk';//pk错误日志
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->financeModel = new FinanceKk();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $data = json_decode($this->option('data'),true);
        if(empty($data))
        {
            die;
        }
        //根据当日开始时间段和结束时间段,主播,房间情况同步到公会房间流水表中
        $start_time = strtotime($data['start_time']);//pk开始时间段
        $end_time =  strtotime($data['end_time']);//pk结束时间段
        $injoinUser = $data['users'];//参加活动的房间+主播
        do {
            //处理更新房间流水表
            foreach($injoinUser as $value){
                //查询送礼记录表数据
                $user_ids = DB::table($this->user_table)->whereIn('uuid',$value['uuids'])->get(['user_id']);
                $user_ids = $this->objToArray($user_ids);
                $userids = array_column($user_ids,'user_id');
                $roomAllValue = DB::table($this->gift_steam_table)
                ->selectRaw('SUM(gift_price*gift_num) as total')
                ->whereIn('user_id',$userids)
                ->whereBetween('change_timestamp',[$start_time,$end_time])
                ->where('room_id',37482)
                ->get();
                $roomAllValue = $this->objToArray($roomAllValue);
                if(empty($roomAllValue)){
                    write_log($this->errorLog,'INFO 无数据','房间'.$value['roomid']);
                    break;
                }
                //根据房间id查询公会id
                $unionInfo = DB::table($this->union_room_table)->where('room_id',$value['roomid'])->first();
                $unionInfo = $this->objToArray($unionInfo);
                if(empty($unionInfo)){
                    write_log($this->errorLog,'INFO 无公会数据','房间'.$value['roomid']);
                    break;
                }
                
                $kk_value = intval($roomAllValue[0]['total']*0.1); //公会结算0.1
                write_log($this->log,'INFO pk期间所有获得金币总数','房间'.$value['roomid'].'总金币数'.$kk_value);
                //给工会长用户加kk值
                $res = $this->financeModel->inc($unionInfo['user_id'],0,$value['roomid'],37,$kk_value,'公会PK排位赛结算-获得金币');
                write_log($this->log,'INFO pk结算状态KK值','房间'.$value['roomid'].'结算金币值状态'.$res);
            }

        } while (false);

    }
    //返回数组
    public function objToArray($document)
    {
        return json_decode(json_encode($document), true);
    }
}